package com.podium.comment.validation.validator;

import com.podium.comment.utils.StringUtils;
import com.podium.comment.validation.annotation.Email;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.util.regex.Pattern;

/**
 * @author 邵文泽
 * @description 邮箱验证器
 * @date 2025/5/14 10:29
 */
public class EmailValidator implements ConstraintValidator<Email,String> {

    private static final Pattern EMAIL = Pattern.compile("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if (!StringUtils.hasLength(value)){
            return true;
        }
        return EMAIL.matcher(value).matches();
    }

}
